package com.jianbo.proxy.service;

import java.util.LinkedHashMap;

import com.jianbo.proxy.conf.jsconfig.ProxyConfig;
import com.jianbo.proxy.entity.ProxyUnit;
import com.jianbo.proxy.util.JsUtil;
import com.jianbo.proxy.util.JsonUtil;

public class ProxyConfigService {

    public static LinkedHashMap<String, ProxyUnit> parse(String json) {
        LinkedHashMap<String, ProxyUnit> result = JsonUtil.parseSortMap(json, ProxyUnit.class);
        return result;
    }

    public static ProxyConfig parseConfig(String json) {
        LinkedHashMap<String, ProxyUnit> map = ProxyConfigService.parse(json);
        ProxyConfig config = new ProxyConfig();
        config.setConf(map);
        return config;
    }

    public static ProxyConfig parseConfigByJs(String content) {
        String code = JsUtil.outputJsCode(content);
        Object result = JsUtil.run(code);
        LinkedHashMap<String, ProxyUnit> map = ProxyConfigService.parse((String)result);
        ProxyConfig config = new ProxyConfig();
        config.setConf(map);
        return config;
    }
    
}
